using System;
using System.ComponentModel;
using System.Security.Permissions;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Collections;

namespace CodeStory.WebControls
{
	[ToolboxItem(false)]
	public class TreeGridRow : TableRow, INamingContainer, IDataItemContainer
	{
		private object dataItem;
		private int dataItemIndex;
		private int displayIndex;
		private bool hasChildRows;

		public TreeGridRow(object dataItem, int dataItemIndex, int displayIndex) {
			this.dataItem = dataItem;
			this.dataItemIndex = dataItemIndex;
			this.displayIndex = displayIndex;
		}

		public int RowIndex {
			get { return displayIndex; }
		}

		public object DataItem {
			get { return dataItem; }
		}
		public int DataItemIndex {
			get { return dataItemIndex; }
		}
		public int DisplayIndex {
			get { return displayIndex; }
		}
		public int Depth {
			get {
				object obj = ViewState["Depth"];
				return obj == null ? 0 : (int)obj;
			}
			set { ViewState["Depth"] = value; }
		}
		public bool HasChildRows {
			get { return hasChildRows; }
			set { hasChildRows = value; }
		}

		protected override bool OnBubbleEvent(object source, EventArgs e) {
			if (e is CommandEventArgs) {
				TreeGridCommandEventArgs args = new TreeGridCommandEventArgs(this, source, (CommandEventArgs)e);
				base.RaiseBubbleEvent(this, args);
				return true;
			}
			return base.OnBubbleEvent(source, e);
		}

		protected override void AddAttributesToRender(HtmlTextWriter writer) {
			if (HasChildRows) {
				writer.AddAttribute("onclick", "javascript:ChangeRowState(this);");
			}
			writer.AddAttribute("__depth", Depth.ToString());
			base.AddAttributesToRender(writer);
		}
	}
}